package org.ccay.excel.exporter.service.impl.internal;

import java.io.IOException;

import org.springframework.beans.BeansException;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import org.ccay.core.log.ILogger;
import org.ccay.core.log.CcayLoggerFactory;

/**
 * Excel导出配置扫描器。
 * 
 * 扫描classpath中的*.excelExport.xml文件，解析它，
 * 并生成DefaultExcelExportSupport实例注册到Spring容器中。
 * 	
 * @author chaowangbang
 * @since Mar 16, 2012
 */
public class ExcelExportSupportScanner implements BeanDefinitionRegistryPostProcessor {
	
	/**
	 * 日志记录器
	 */
	private ILogger logger = CcayLoggerFactory.getLogger(ExcelExportSupportScanner.class);
	
	/**
	 * 要扫描的*.excelExport.xml文件所在的包
	 */
	private String basePackage = "classpath*:config/*.excelExport.xml";
	
	/** 设置basePackage
	 * @author chaowangbang
	 * @since Mar 16, 2012
	 * @param basePackage
	 */
	public void setBasePackage(String basePackage) {
		this.basePackage = basePackage;
	}

	public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
		ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
		try {
			Resource[] resources = resourcePatternResolver.getResources(basePackage);
			for (Resource resource : resources) {
				BeanDefinition beanDefinition = buildBeanDefinition(resource);
				String beanName = resource.getFilename();
				beanName = beanName.substring(0, beanName.length()-16);
				beanDefinitionRegistry.registerBeanDefinition("IExcelExportSupport."+beanName, beanDefinition);
			}
			
		} catch (IOException e) {
			logger.error(e);
		}
	}

	/* (non-Javadoc)
	 * @see org.springframework.beans.factory.config.BeanFactoryPostProcessor#postProcessBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)
	 */
	public void postProcessBeanFactory(ConfigurableListableBeanFactory arg0) throws BeansException {
	}
	
	/** 构造一个BeanDefinition	
	 * @author chaowangbang
	 * @since Mar 16, 2012
	 * @param resource
	 * @return
	 */
	private BeanDefinition buildBeanDefinition(Resource resource) {
		GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
		beanDefinition.setBeanClass(DefaultExcelExportSupport.class);
		beanDefinition.setLazyInit(true);
		beanDefinition.setInitMethodName("initialize");
		beanDefinition.setResource(resource);
		beanDefinition.setScope(BeanDefinition.SCOPE_SINGLETON);
		//beanDefinition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);
		
		MutablePropertyValues propertyValues = new MutablePropertyValues();
		propertyValues.add("resource", resource);
		beanDefinition.setPropertyValues(propertyValues);
		
		return beanDefinition;
	}
	

}
